package com.yunhe.tianhe.rentcars.service.permission.impl;

import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.commons.util.StringUtils;
import com.yunhe.tianhe.rentcars.dao.permission.ResourceDao;
import com.yunhe.tianhe.rentcars.dao.permission.RoleDao;
import com.yunhe.tianhe.rentcars.dto.TreeNode;
import com.yunhe.tianhe.rentcars.entity.permission.Menu;
import com.yunhe.tianhe.rentcars.entity.permission.Resource;
import com.yunhe.tianhe.rentcars.service.permission.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lanyangyun
 * @version V1.0
 * @Description: //TODO
 * @date 2017/10/19 0019 上午 10:47
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public void delete(Long permisId) {
        resourceDao.delete(permisId);
    }

    @Override
    public Resource find(Long permisId) {
        return resourceDao.find(permisId);
    }

    @Override
    public List<Resource> findBtn(Long permisId, Long roleId) {
        return null;
    }

    @Override
    public void save(Resource permission) {
        resourceDao.save(permission);
    }

    @Override
    public void deleteBatch(String ids) {
        if (StringUtils.notEmpty(ids)){
            String[] strings = ids.split(",");
            for (String s: strings){
                Long id = Long.valueOf(s);
                this.delete(id);
            }
        }
    }

    @Override
    public void update(Resource permission) {
        resourceDao.update(permission);
    }


    @Override
    public List<Resource> findAll() {
        return resourceDao.findAll();
    }

    @Override
    public Pageable<Resource> findPage(Integer offset, Integer size) {
        Pageable<Resource> pageable = new Pageable();
        int count = resourceDao.findCount();
        List<Resource> data = null;
        if (count>0){
            pageable.setPage(offset/size+1);
            pageable.setSize(size);
            data = resourceDao.findList(offset,size);
        }
        pageable.setTotal(new Long(count));
        pageable.setRows(data);
        return pageable;
//        return permissionDao.findPage(offset,size);
    }

    @Override
    public List<Resource> findByRole(Long roleId) {
        return resourceDao.findByRole(roleId);
    }


    @Override
    public List<Resource> findByType(Integer type) {
        return resourceDao.findByType(type);
    }

    @Override
    public List<Resource> findByParentId(Long parentId) {
       return resourceDao.findByParentId(parentId);
    }

    @Override
    public List<TreeNode> findTreeNodes(Long roleId) {
        List<TreeNode> treeNodes = new ArrayList<>();
        List<Resource> permissionList = this.findAll();
        List<Integer> ids = roleDao.findPermissionIdsByRoleId(roleId);
        if (null!=permissionList){
            for (Resource p:permissionList){
                TreeNode treeNode = new TreeNode(p.getPermisId(), p.getParentId(), p.getPermisName());
                if (null!=ids&&ids.contains(p.getPermisId())){
                    treeNode.setChecked(Boolean.TRUE);
                }
               treeNodes.add(treeNode);
           }
        }
        return treeNodes;
    }

    @Override
    public List<Resource> findAllPermission(Integer userId) {
        return null;
    }

    @Override
    public List<Menu> findResourceMenu(long roleid) {
       List<Menu> menuList=new ArrayList<>();
        Map<Long,Menu> menuMap=new HashMap<>();
        List<Resource> modelePerm = resourceDao.findResourceMenu(roleid, 1);
        List<Resource> pagePerm = resourceDao.findResourceMenu(roleid, 2);
        for(Resource p:modelePerm){
            Menu menu=new Menu(p.getPermisId(),p.getPermisName(),p.getRelateUrl());
            menuList.add(menu);
            menuMap.put(p.getPermisId(),menu);
        }

        for(Resource p:pagePerm){
            Menu menu=new Menu(p.getPermisId(),p.getPermisName(),p.getRelateUrl());
            Menu parentMenu=menuMap.get(p.getParentId());
            if(parentMenu!=null){
                parentMenu.getSubmenus().add(menu);

            }
        }
    return menuList;}
}
